package com.voxel.sense.infomsg.module.trace.details;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.utils.SizeUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.model.api.response.ClockInsDetailsEntity;


public class ClockInDetItemDivider extends RecyclerView.ItemDecoration {

    private Drawable mDivider;

    public ClockInDetItemDivider(int resId) {
        mDivider = Factory.app().getResources().getDrawable(resId);

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent) {
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        drawHorizontalLine(c, parent, state);
    }

    //画横线, 这里的parent其实是显示在屏幕显示的这部分
    public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            //获得child的布局信息
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();

            //判断当前的item是否显示的是天数，若是则顶部边框设置为10dp
            if (i > 0) {
                int childLayoutPosition = parent.getChildLayoutPosition(child);
                RecyclerAdapter adapter = (RecyclerAdapter) parent.getAdapter();
                //获取上一个item的数据类型
                Object o = adapter.getItems().get(childLayoutPosition);
                Object o1 = adapter.getItems().get(childLayoutPosition - 1);
                if (o instanceof ClockInsDetailsEntity.CoordinateListBean &&
                        (o1 instanceof ClockInsDetailsEntity.CoordinateListBean.ValuesBean || o1 instanceof String[])) {
                    int childTop = child.getTop() + params.topMargin - SizeUtils.dp2px(9);
                    mDivider.setBounds(left, childTop, right, child.getTop() + params.topMargin);
                    mDivider.draw(c);
                }
            }
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
            //Log.d("wnw", left + " " + top + " "+right+"   "+bottom+" "+i);
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //判断当前的item是否显示的是天数，若是则顶部边框设置为10dp
        int childLayoutPosition = parent.getChildLayoutPosition(view);
        if (childLayoutPosition > 0) {
            RecyclerAdapter adapter = (RecyclerAdapter) parent.getAdapter();
            //获取上一个item的数据类型
            Object o = adapter.getItems().get(childLayoutPosition);
            Object o1 = adapter.getItems().get(childLayoutPosition - 1);
            if (o instanceof ClockInsDetailsEntity.CoordinateListBean && (o1 instanceof ClockInsDetailsEntity.CoordinateListBean.ValuesBean || o1 instanceof String[])) {
                outRect.set(0, SizeUtils.dp2px(9), 0, mDivider.getIntrinsicHeight());
            } else {
                outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
            }
        } else {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        }
    }
}
